package com.rongji.dfish.hello.dao;

import com.rongji.dfish.base.Pagination;
import com.rongji.dfish.base.util.Utils;
import com.rongji.dfish.framework.hibernate5.dao.impl.FrameworkDao4Hibernate;
import com.rongji.dfish.hello.constant.DemoConstants;
import com.rongji.dfish.hello.entity.SysUser;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class UserDao extends FrameworkDao4Hibernate<SysUser, String> {

    public List<SysUser> list(Pagination pagination, Map<String, Object> params) {

        StringBuilder hql = new StringBuilder();
        // FIXME 机构编号空判断
        List<Object> args = new ArrayList<>();
        hql.append("FROM SysUser t WHERE t.orgId=?");
        args.add(params.get("orgId"));


        String userName = (String) params.get("userName");
        String loginName = (String) params.get("loginName");
        String gender = (String) params.get("gender");
        if (Utils.notEmpty(userName)) {
            hql.append(" AND t.userName LIKE ?");
            args.add("%" + userName + "%");
        }
        if (Utils.notEmpty(loginName)) {
            hql.append(" AND t.loginName = ?");
            args.add(loginName);
        }
        if (Utils.notEmpty(gender)) {
            hql.append(" AND t.gender=?");
            args.add(gender);
        }

        hql.append(" AND t.userStatus!=?");
        args.add(DemoConstants.STATUS_DELETE);

        hql.append(" ORDER BY t.userId DESC");

        // 不做空指针
        return queryForList(hql.toString(), pagination, args.toArray());
    }

    public int updateStatus(String userId, String userStatus) {
        // 非空判断
        return bulkUpdate("UPDATE SysUser t SET t.userStatus=? WHERE t.userId=?", new Object[]{userStatus, userId});
    }

}
